<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PostModel
 * 文章关联模型
 * @author yihua
 */

namespace Common\Model;

use Think\Model;

class ItemModel extends Model {

    //验证规则项
    protected $_validate = array(
        array('alias', 'require', '请输入别名', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        array('item_name', 'require', '请输入分类名', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        array('alias', 'checkItemAlias', '分类别名的格式不正确', self::EXISTS_VALIDATE, 'callback', self::MODEL_BOTH),
        array('alias', 'isItemAliasNew', '分类别名已被使用', self::EXISTS_VALIDATE, 'callback', self::MODEL_BOTH), // 自定义函数验证密码格式
        array('item_name', 'isItemNameNew', '分类名已被使用', self::EXISTS_VALIDATE, 'callback', self::MODEL_BOTH),
    );
    //自动添加项
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('modify_time', NOW_TIME, self::MODEL_BOTH)
    );

    /**
     * 插入分类
     * @param type $data
     * @return boolean
     */
    public function insert($data) {
        $item = $this->create($data);
        if (empty($item)) {
            return false;
        }
        $res = $this->add($item);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 更新分类
     * @param type $data
     * @return boolean
     */
    public function modify($data) {
        $item = $this->create($data);
        if (empty($item)) {
            return false;
        }
        $res = $this->where(array('item_id' => $item['item_id']))->save($item);
        if ($res === false) {
            $this->error = "保存失败！";
            return false;
        } else {
            return true;
        }
    }

    /**
     * 分类列表（普通的数组）
     */
    public function loadList($map) {
        $list['count'] = $this->where($map)->count();
        $all = $this->where($map)->select();
        $list['list'] = $this->makeList($all);
        return $list;
    }

    private function makeList($list, $pid = 0, $level = 0, $arr = array()) {
        foreach ($list as $key => $value) {
            if ($value['pid'] == $pid) {
                $arr[$value['item_id']] = $value;
                $arr[$value['item_id']]['level'] = $level;
                $arr = $this->makeList($list, $value['item_id'], $level + 1, $arr);
            }
        }
        return $arr;
    }

    /**
     * 检测标签别名的格式
     * @param type $alias
     * @return boolean
     */
    public function checkItemAlias($alias) {
        $pattern = '/^(\w)+$/';
        if (preg_match($pattern, $alias)) {
            return true;
        } else {
            return false;
        }
    }

    public function isItemAliasNew($alias) {
        return !$this->isItemAliasUsed($alias);
    }

    public function isItemNameNew($name) {
        return !$this->isItemNameUsed($name);
    }

    /**
     * 检测标签别名的格式
     * @param type $alias
     * @return boolean
     */
    public function isItemAliasUsed($alias, $id) {
        $map['alias'] = $alias;
        if (!empty($id)) {
            $map['item_id'] = array('neq', $id);
        } else {
            $map['item_id'] = array('neq', I('item_id'));
        }
        if ($this->where($map)->find()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检测标签别名的格式
     * @param type $alias
     * @return boolean
     */
    public function isItemNameUsed($name,$id) {
        $map['item_name'] = $name;
        if (!empty($id)) {
            $map['item_id'] = array('neq', $id);
        } else {
            $map['item_id'] = array('neq', I('item_id'));
        }
        if ($this->where($map)->find()) {
            return true;
        } else {
            return false;
        }
    }

}
